<?php


namespace app\suguan\controller;


use think\Db;
use think\Exception;
use think\exception\PDOException;

class Tiezi extends Base
{
    public function index()
    {
        $page = request()->param('page', '1', 'intval');
        $limit = request()->param('limit', 10, 'intval');
        $house_id = $this->house_id();
        $where = [];
        $where[] = ['tz.house_id', '=', $house_id];
        $list = model('Tiezi')->alias('tz')->leftJoin('member m', 'm.uid = tz.uid')
            ->where($where)->page($page, $limit)
            ->field('tz.id,tz.title,tz.create_time,tz.status,tz.image_id,tz.uid,m.username,m.student_no')
            ->order('tz.id desc')->select();
        foreach ($list as $k => $v) {
            $list[$k]['image_url'] = model('File')->getPath($v['image_id']);
            $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            $tmp = Db::name('tiezi_liuyan')->where('tz_id',$v['id'])->alias('ly')
                                        ->leftJoin('member m','m.uid = ly.uid')
                                        ->field('ly.id,ly.title,ly.create_time,ly.uid,m.username,m.student_no')->select();
            foreach ($tmp as $kk=>$vv){
                $tmp[$kk]['create_time'] = date('Y-m-d H:i:s', $vv['create_time']);
            }
            $list[$k]['tizi_liuyan'] = $tmp;
        }
        $total = model('Tiezi')->alias('tz')->leftJoin('member m', 'm.uid = tz.uid')
            ->where($where)->count();
        $data = [
            'list' => $list,
            'total' => $total
        ];
        return $this->ok($data);
    }

    public function changeStatus() {
        $params = request()->only(['id','status']);
        if(!isset($params['id'])){
            return $this->fail('参数错误');
        }
        $res = model('Tiezi')->where(['id'=>$params['id']])->update($params);
        if ($res) {
            return $this->ok('修改成功');
        } else {
            return $this->fail('修改失败');
        }
    }

    public function delete() {
        $id = request()->param('id', '');
        Db::startTrans();
        try {
            $res = model('Tiezi')->where('id', $id)->where('house_id', $this->house_id())->delete();
            $res1 = Db::name('tiezi_liuyan')->where('tz_id', $id)->delete();
            Db::commit();
            return $this->ok('删除成功');
        } catch (Exception $e) {
            return $this->fail($e->getMessage());
        }
    }

    public function deleteLiuyan() {
        $id = request()->param('id', '');
        $res = Db::name('tiezi_liuyan')->where('id', '=', $id)->delete();
        if ($res) {
            return $this->ok('删除成功');
        } else {
            return $this->fail('删除失败');
        }
    }

    public function detail(){
        $id = request()->param('id', 0, 'intval');
        if (!$id) {
            return $this->fail('参数错误');
        }
        $house_id =  $this->house_id();
        $detail = model('Tiezi')->where(['house_id' => $house_id,'id'=>$id])->find();
        if (empty($detail)){
            return $this->fail('该帖子信息不存在');
        }
        $user = model('Member')->where('uid',$detail['uid'])->find();
        $detail['student_no'] = isset($user['student_no'])? $user['student_no'] : '';
        $detail['username'] = isset($user['username'])? $user['username'] : '';
        $detail['image_url'] = model('File')->getPath($detail['image_id']);
        $detail['create_time'] = date('Y-m-d H:i:s', $detail['create_time']);
        return $this->ok($detail);
    }
}